/// Author: Samle
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controllers/words_controller.dart';

/// 特殊自定义组件
import '../../../../util/SearchFence.dart';
import '../../../../util/words_list_view.dart';

class WordsView extends GetView<WordsController> {
  void checkDel(context, value) async {
    Get.defaultDialog(
      title: '确认删除？',
      radius: 10.0,
      barrierDismissible: false,
      content: const Text("请确认是否删除该条数据！"),
      confirm: MaterialButton(
        color: Colors.black,
        textColor: Colors.white,
        child: const Text('确认',
            style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600)),
        onPressed: () async {
          await controller.delWords(value);
          Get.back();
          controller.onInit();
        },
      ),
      cancel: MaterialButton(
        color: Colors.white,
        textColor: Colors.black87,
        child: const Text('取消',
            style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600)),
        onPressed: () {
          Get.back();
        },
      ),
    );
  }
  String truncateString(String text, int maxLength) {
    if (text.length <= maxLength) {
      return text;
    } else {
      return text.substring(0, maxLength) + '...';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

          ///leading: IconButton(
          ///  icon: const Icon(Icons.density_medium_sharp),
          ///  onPressed: (){},
          ///),
          elevation: 0,
          backgroundColor: Colors.black87,
          title: Obx(()=>Text(controller.bookForm.value['name']!=null?truncateString(controller.bookForm.value['name'],10):'')),
          centerTitle: true),
      body: Container(
        decoration: const BoxDecoration(color: Colors.white),
        child: ConstrainedBox(
          /// 约束盒子
          constraints: const BoxConstraints.expand(),

          /// 不指定高和宽时则铺满整个屏慕
          child: Stack(
            alignment: Alignment.center,

            /// 指定对齐方式为居中
            children: <Widget>[
              /// 子组件列表
              controller.obx((state) => Center(
                    child: controller.wordsList.value.isNotEmpty
                        ?

                        /// MyListView 是一个自定义的列表显示组件
                        /// https://blog.csdn.net/weixin_40929065/article/details/123797218
                        WordsListView(
                            datas: controller.wordsList.value,
                            onPressed: (item) {
                              print(item);
                              print("点击查看");
                            },
                            doublePressed: (item) {
                              if (controller.bookForm.value['source'] != 'default') {
                                print(item);
                                print("双击编辑");
                                controller.dataForm = item;
                                controller.toEditPage();
                              }
                            },
                            longPressed: (item) {
                              if (controller.bookForm.value['source'] != 'default') {
                                print(item);
                                print("长按删除");
                                checkDel(context, item);
                              }
                            },
                            onTap: (id) {
                              print(id);
                              print("点击获取id");
                            },
                            dropdown: () {
                              controller.getWordsList();
                            },
                            pullup: () {
                              print("上拉加载回调");
                              controller.getWordsListMore();
                            },
                          )
                        : const Text("暂无记录"),
                  )),
              Positioned(
                top: 0.0,
                child:

                    /// MySearchFence 是一个自定义的搜索组件
                    /// https://blog.csdn.net/weixin_40929065/article/details/123638686
                    SearchFence(
                  onSearch: (value) { controller.search.text = value;controller.getWordsList(); },
                  hasRightIcon: false,
                  rightIcon: Icons.flip,
                  onIcon: (value) { controller.search.text = value;controller.getWordsList(); },
                  onChange: (value) { controller.search.text = value;controller.getWordsList(); },
                  onClean: (value) { controller.search.text = value;controller.getWordsList(); },
                ),
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: Obx(()=>controller.bookForm.value['source'] != 'default'?FloatingActionButton(
        // backgroundColor: Colors.transparent,
        backgroundColor: Colors.white70,
        foregroundColor: Colors.black87,
        elevation: 0,
        onPressed: () {
          controller.toEditPage();
        },
        child: const Icon(Icons.post_add_outlined),
      ):const SizedBox()),
    );
  }
}
